package edu.washington.cs.sounddetector.server.database;

import java.util.Collection;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Queries;
import javax.jdo.annotations.Query;

/**
 * Database table for saving registered receiver (user) phones.
 * 
 * @author the.dylan.price@gmail.com
 * 
 */
@PersistenceCapable
@Queries({
        @Query(
                name = "QUERY_BY_USERID_PHONE",
                language = "JDOQL",
                value = "SELECT UNIQUE FROM edu.washington.cs.sounddetector.server.database.ReceiverPhone WHERE userId == :userId && phoneId == :phoneId"),
        @Query(
                name = "QUERY_BY_USER",
                language = "JDOQL",
                value = "SELECT FROM edu.washington.cs.sounddetector.server.database.ReceiverPhone WHERE userId == :userId"),
        @Query(
                name = "QUERY_BY_REGID",
                language = "JDOQL",
                value = "SELECT FROM edu.washington.cs.sounddetector.server.database.ReceiverPhone WHERE registrationId == :registrationId") })
public class ReceiverPhone
{
    /**
     * Named Query
     * 
     * @parm userId (String)
     * @param phoneId
     *            (String)
     */
    public static final String QUERY_BY_USERID_PHONE = "QUERY_BY_USERID_PHONE";

    /**
     * Named Query.
     * 
     * @param userId
     *            (String)
     */
    public static final String QUERY_BY_USER = "QUERY_BY_USER";

    /**
     * Named Query.
     * 
     * @param registrationId
     *            (String)
     */
    public static final String QUERY_BY_REGID = "QUERY_BY_REGID";

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.NATIVE)
    private long id;
    private String phoneId;
    private String userId;
    private String registrationId;

    public ReceiverPhone(String phoneId, String registrationId)
    {
        // TODO: change this when we implement users
        this.userId = "1";
        this.phoneId = phoneId;
        this.registrationId = registrationId;
    }

    public long getId()
    {
        return id;
    }

    /**
     * @return the phoneId
     */
    public String getPhoneId()
    {
        return phoneId;
    }

    /**
     * @return the registrationId
     */
    public String getRegistrationId()
    {
        return registrationId;
    }

    /**
     * @param phoneId
     *            the phoneId to set
     */
    public void setPhoneId(String phoneId)
    {
        this.phoneId = phoneId;
    }

    /**
     * @param registrationId
     *            the registrationId to set
     */
    public void setRegistrationId(String registrationId)
    {
        this.registrationId = registrationId;
    }

    /**
     * @return the userId
     */
    public String getUserId()
    {
        return userId;
    }

    /**
     * @param userId
     *            the userId to set
     */
    public void setUserId(String userId)
    {
        this.userId = userId;
    }

    /**
     * Convenience method for {@link #QUERY_BY_PHONE}.
     */
    public static ReceiverPhone query(PersistenceManager pm, String userId, String phoneId)
    {
        javax.jdo.Query query = pm.newNamedQuery(ReceiverPhone.class, QUERY_BY_USERID_PHONE);
        ReceiverPhone phone = (ReceiverPhone) query.execute(userId, phoneId);
        return phone;
    }

    public static Collection<ReceiverPhone> query(PersistenceManager pm, String userId)
    {
        javax.jdo.Query query = pm.newNamedQuery(ReceiverPhone.class, QUERY_BY_USER);
        @SuppressWarnings("unchecked")
        Collection<ReceiverPhone> phones = (Collection<ReceiverPhone>) query.execute(userId);
        return phones;

    }
}
